package com.yunxi.service.dubboService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.OtdOrderCir;
import com.yunxi.otd.pojo.OtdOrderCirEditVO;
import com.yunxi.otd.pojo.OtdOrderCirScheduleQueryVO;
import com.yunxi.otd.service.OtdOrderCirDubboService;
import com.yunxi.service.localService.CodeCreateService;
import com.yunxi.service.localService.otd.OtdInterfaceConfigService;
import com.yunxi.service.localService.otd.OtdOrderCirService;
import com.yunxi.service.localService.otd.OtdOrderSoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;

/**
 * <p>
 * CIR生产订单 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-07-15
 */
public class OtdOrderCirDubboServiceImpl implements OtdOrderCirDubboService {

    @Autowired
    @Qualifier(value = "otdOrderCirServiceImpl")
    OtdOrderCirService otdOrderCirService;

    @Autowired
    @Qualifier(value = "otdOrderSoServiceImpl")
    OtdOrderSoService soService;

    @Autowired
    @Qualifier("codeCreateServiceImpl")
    CodeCreateService code;


    @Autowired
    @Qualifier("otdInterfaceConfigServiceImpl")
    OtdInterfaceConfigService interfaceConfigService;

    @Override
    public ApiResult findCirSchedule(OtdOrderCirScheduleQueryVO queryVO) {
        return ResponseUtil.ok(otdOrderCirService.findCirOrderSchedule(queryVO));
    }

    /**
     * CIR计划分周查询
     *
     * @param queryVO 查询条件
     * @return 分页对象
     */
    @Override
    public ApiResult findMonthCirWeekly(OtdOrderCirScheduleQueryVO queryVO) {
        return ResponseUtil.ok(otdOrderCirService.findMonthCirWeekly(queryVO));
    }

    /**
     * CIR订单日排程修改数量
     *
     * @param editVO 编辑VO
     * @return ApiResult
     */
    @Override
    public ApiResult editOrderNum(OtdOrderCirEditVO editVO) {
        // TODO 待反馈时不能修改


        //查询原数量
        LambdaQueryWrapper<OtdOrderCir> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OtdOrderCir::getProductCode, editVO.getProductCode()).eq(OtdOrderCir::getWorks, editVO.getWorks())
            .eq(OtdOrderCir::getDemandDate, editVO.getDemandDate());
        if (StringUtils.isEmpty(editVO.getCustomPackCode())) {
            wrapper.isNull(OtdOrderCir::getCustomPackCode);
        } else {
            wrapper.eq(OtdOrderCir::getCustomPackCode, editVO.getCustomPackCode());
        }
        OtdOrderCir orderCir = otdOrderCirService.getOne(wrapper);
        if (orderCir == null) {
            orderCir = new OtdOrderCir();
            orderCir.setPlanNo(code.createCode("MP", "yyMMdd", 5));
            orderCir.setProductCode(editVO.getProductCode());
            orderCir.setCustomPackCode(editVO.getCustomPackCode());
            orderCir.setCarName(soService.generateCarName(editVO.getCustomPackCode(), editVO.getCustomPackCode()));
            orderCir.setDemandDate(editVO.getDemandDate());
            orderCir.setWorks("AP21");
            orderCir.setSapState("W");
        }
        orderCir.setOrderNum(editVO.getNum());
        otdOrderCirService.saveOrUpdate(orderCir);
        return ResponseUtil.ok();
    }

    @Override
    public ApiResult getNoEditDays(String yearMonth) {
        return ResponseUtil.ok(interfaceConfigService.findAlreadySendDayList(yearMonth, "CIR"));
    }
}
